package com.jiazhong.boot.alipay.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.jiazhong.boot.alipay.bean.Alipay;
import com.jiazhong.boot.alipay.service.AlipayService;
import com.jiazhong.boot.commons.util.JsonResult;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Value("${appId}")
    private String appId;
    @Value("${privateKey}")
    private String privateKey;
    @Value("${publicKey}")
    private String publicKey;
    @Value("${notifyUrl}")
    private String notifyUrl;
    @Value("${returnUrl}")
    private String returnUrl;
    @Value("${signType}")
    private String signType;
    @Value("${charset}")
    private String charset;
    @Value("${gatewayUrl}")
    private String gatewayUrl;

    @SneakyThrows
    @Override
    public String pay(Alipay alipay) {
        log.info("开始执行支付操作：{}", alipay);
        // 产生支付宝的客户端
        AlipayClient client = new DefaultAlipayClient(gatewayUrl, appId, privateKey,
                "json", charset, publicKey, signType);
        // 产生交易对象
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 设置回调
        request.setReturnUrl(returnUrl);
        request.setNotifyUrl(notifyUrl);
        // 设置支付参数
        String s = JSONArray.toJSONString(alipay);
        request.setBizContent(s);
        // 执行
        return client.pageExecute(request).getBody();
    }

    @SneakyThrows
    @Override
    public String refund(Alipay alipay) {
        log.info("开始执行退款操作：{}", alipay);
        AlipayClient client = new DefaultAlipayClient(gatewayUrl, appId, privateKey,
                "json", charset, publicKey, signType);
        // 产生退款对象
        AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest();
        String s = JSONArray.toJSONString(alipay);
        refundRequest.setBizContent(s);
        AlipayTradeRefundResponse refundResponse = client.execute(refundRequest);
        if (refundResponse.isSuccess()) {
            return "成功";
        }
        return "失败";
    }

    @SneakyThrows
    @Override
    public String query(Alipay alipay) {
        AlipayClient client = new DefaultAlipayClient(gatewayUrl, appId, privateKey,
                "json", charset, publicKey, signType);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent(JSONArray.toJSONString(alipay));
        AlipayTradeQueryResponse response = client.execute(request);
        return response.getTradeStatus();
    }
}
